const { cwd } = require("node:process");
const path = require("path");
const compressing = require("compressing");

const defaultOption = {
  sourceName: "dist",
  type: "zip",
  targetName: "dist",
};

function compression(options = defaultOption) {
  // const { sourceName, targetName = "dist" } = options;
  const sourceName = options.sourceName ?? "dist";
  const type = options.type ?? "zip";
  const targetName = options.targetName ?? "dist";

  const targetPath = path.resolve(cwd(), sourceName);
  return {
    name: "compression",
    buildStart() {
      console.log("buildStart 需要删除dist目录吗");
    },
    closeBundle() {
      console.log("closeBundle");
      compressing.zip
        .compressDir(targetPath, `${targetName}.${type}`)
        .then((res) => {
          console.log(res);
        })
        .catch((err) => {
          console.log(err);
        });
    },
  };
}
// export default compression;
module.exports = {
  compression,
};
